import Vue from 'vue'
import VueRouter from 'vue-router'
/* 一级路由 */
import Layout from '@/views/layout.vue'
import Login from '@/views/login.vue'
import Register from '@/views/register.vue'
import Detail from '@/views/detail.vue'
import City from '@/views/city.vue'

// import BaiDuMap from '@/components/BaiDuMap.vue'
/* 二级路由 */
import Like from '@/views/Two/like.vue'
import Article from '@/views/Two/article.vue'
import Collect from '@/views/Two/collect.vue'
import User from '@/views/Two/user.vue'
import Users from '@/views/Two/users.vue'
// import { getToken } from '@/utils/stroage'
Vue.use(VueRouter)
const routes = [
  { path: '/detail', component: Detail },
  { path: '/login', component: Login },
  { path: '/register', component: Register },
  { path: '/city', component: City },
  { path: '/map', component: () => import('@/components/BaiDuMap.vue') },
  {
    path: '/add',
    component: () => import('@/views/add')
  },
  { path: '/add/search', component: () => import('@/views/add/search') },
  {
    path: '/',
    component: Layout,
    redirect: '/article',
    /* 二级路由哈希地址 */
    children: [
      { path: '/article', component: Article },
      { path: '/like', component: Like },
      { path: '/collect', component: Collect },
      { path: '/user', component: User },
      { path: '/users', component: Users }
    ]
  }
]

const router = new VueRouter({
  routes
})

// const whiteList = ['/login', '/register'] // 白名单列表，记录无需权限访问的所有页面

// router.beforeEach((to, from, next) => {
//   const token = getToken()
//   // 如果有token，直接放行
//   if (token) {
//     next()
//   } else {
//     // 没有token的人, 看看你要去哪
//     // (1) 访问的是无需授权的页面（白名单），也是放行
//     //     就是判断，访问的地址，是否在白名单数组中存在 includes
//     if (whiteList.includes(to.path)) {
//       next()
//     } else {
//       // (2) 否则拦截到登录
//       next('/login')
//     }
//   }
// })
export default router
